package com.zhanqiao.controller;

import com.google.gson.Gson;
import com.zhanqiao.service.OnSaleService;
import com.zhanqiao.service.SelectOnSaleProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/editsale.action")
public class EditSaleController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        String pid = req.getParameter("pid");
        String pname = req.getParameter("pname");
        String psaleprice = req.getParameter("psaleprice");
        String pcount = req.getParameter("pcount");
        SelectOnSaleProductService selectOnSaleProductService = new SelectOnSaleProductService();
        int i = 0;
        try {
            i = selectOnSaleProductService.editProduct(Integer.parseInt(pid), pname, Integer.parseInt(psaleprice), Integer.parseInt(pcount));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Map message = new HashMap();
        System.out.println("i=" + i);
        if (i > 0) {
            message.put("msg", "编辑成功");
        } else {
            message.put("msg", "编辑失败");
        }
        String result = new Gson().toJson(message);
        resp.getWriter().write(result);

    }
}
